package org.jeecg.modules.controller.tcmTank;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.modules.searchHistory.entity.SearchHistory;
import org.jeecg.common.modules.searchHistory.service.ISearchHistoryService;
import org.jeecg.common.modules.tcmTank.entity.TcmTank;
import org.jeecg.common.modules.tcmTank.service.ITcmTankService;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
* @Description: 中医智库医生
* @Author: jeecg-boot
* @Date:   2022-01-17
* @Version: V1.0
*/
@Api(tags="中医智库医生")
@RestController
@RequestMapping("/api/v1/tcmTank/tcmTank")
@Slf4j
public class TcmTankController extends JeecgController<TcmTank, ITcmTankService> {
   @Autowired
   private ITcmTankService tcmTankService;
   @Autowired
   private ISearchHistoryService searchHistoryService;

   /**
    * 分页列表查询
    *
    * @param pageNo
    * @param pageSize
    * @param req
    * @return
    */
   @AutoLog(value = "中医智库医生-分页列表查询")
   @ApiOperation(value="中医智库医生-分页列表查询", notes="中医智库医生-分页列表查询")
   @GetMapping(value = "/list")
   public Result<?> queryPageList(TcmTankVO tcmTankVO,
                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                  HttpServletRequest req) {
       TcmTank tcmTank = new TcmTank();
       BeanUtils.copyProperties(tcmTankVO,tcmTank);
       tcmTank.setKeyWord(null);
       QueryWrapper<TcmTank> queryWrapper = QueryGenerator.initQueryWrapper(tcmTank, req.getParameterMap());
       queryWrapper.eq("status",0);
       if(tcmTankVO.getKeyWord()!=null){
           queryWrapper.like("doctor_name",tcmTankVO.getKeyWord()).or().like("key_word",tcmTankVO.getKeyWord());
           SearchHistory searchHistory = new SearchHistory();
           searchHistory.setOpenid(tcmTankVO.getOpenid());
           searchHistory.setHotReach(tcmTankVO.getKeyWord());
           addHotSeach(searchHistory);

       }
       queryWrapper.orderByAsc("sort");
       Page<TcmTank> page = new Page<TcmTank>(pageNo, pageSize);
       IPage<TcmTank> pageList = tcmTankService.page(page, queryWrapper);
       return Result.ok(pageList);
   }
    private static String getType(Object a) {
        return a.getClass().toString();

    }
   /**
    *   添加
    *
    * @param tcmTank
    * @return
    */
   @AutoLog(value = "中医智库医生-添加")
   @ApiOperation(value="中医智库医生-添加", notes="中医智库医生-添加")
   @PostMapping(value = "/add")
   public Result<?> add(@RequestBody TcmTank tcmTank) {
       tcmTankService.save(tcmTank);
       return Result.ok("添加成功！");
   }


   /**
    *  编辑
    *
    * @param tcmTank
    * @return
    */
   @AutoLog(value = "中医智库医生-编辑")
   @ApiOperation(value="中医智库医生-编辑", notes="中医智库医生-编辑")
   @PutMapping(value = "/edit")
   public Result<?> edit(@RequestBody TcmTank tcmTank) {
       tcmTankService.updateById(tcmTank);
       return Result.ok("编辑成功!");
   }

   /**
    *   通过id删除
    *
    * @param id
    * @return
    */
   @AutoLog(value = "中医智库医生-通过id删除")
   @ApiOperation(value="中医智库医生-通过id删除", notes="中医智库医生-通过id删除")
   @DeleteMapping(value = "/delete")
   public Result<?> delete(@RequestParam(name="id",required=true) String id) {
       TcmTank tcmTank = tcmTankService.getById(id);
       tcmTank.setStatus("1");
       tcmTankService.updateById(tcmTank);
       return Result.ok("删除成功!");
   }

   /**
    *  批量删除
    *
    * @param ids
    * @return
    */
   @AutoLog(value = "中医智库医生-批量删除")
   @ApiOperation(value="中医智库医生-批量删除", notes="中医智库医生-批量删除")
   @DeleteMapping(value = "/deleteBatch")
   public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
       this.tcmTankService.removeByIds(Arrays.asList(ids.split(",")));
       return Result.ok("批量删除成功!");
   }

   /**
    * 通过id查询
    *
    * @param id
    * @return
    */
   @AutoLog(value = "中医智库医生-通过id查询")
   @ApiOperation(value="中医智库医生-通过id查询", notes="中医智库医生-通过id查询")
   @GetMapping(value = "/queryById")
   public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
       TcmTank tcmTank = tcmTankService.getById(id);
       if(tcmTank==null) {
           return Result.error("未找到对应数据");
       }
       return Result.ok(tcmTank);
   }

   /**
   * 导出excel
   *
   * @param request
   * @param tcmTank
   */
   @RequestMapping(value = "/exportXls")
   public ModelAndView exportXls(HttpServletRequest request, TcmTank tcmTank) {
       return super.exportXls(request, tcmTank, TcmTank.class, "中医智库医生");
   }

   /**
     * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
   @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
   public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
       return super.importExcel(request, response, TcmTank.class);
   }
    private Result<?> addHotSeach(@RequestBody SearchHistory searchHistory) {
        searchHistoryService.save(searchHistory);
        return Result.ok("添加成功！");
    }

    /**
     * 分页列表查询
     *
     * @param searchHistory
     * @param req
     * @return
     */
    @AutoLog(value = "热搜字表-分页列表查询")
    @ApiOperation(value="热搜字表-分页列表查询", notes="热搜字表-分页列表查询")
    @GetMapping(value = "/hotSeachList")
    public Result<?> hotSeachList(SearchHistory searchHistory,HttpServletRequest req) {
        QueryWrapper<SearchHistory> queryWrapper = QueryGenerator.initQueryWrapper(searchHistory, req.getParameterMap());
        return Result.ok( searchHistoryService.list(queryWrapper.lambda().eq(SearchHistory::getOpenid,searchHistory.getOpenid())));
    }
}
